package com.abc.invoke.client;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.abc.invoke.bean.User;

public class SpringInvokerClient {

	private JFrame mainFrame = new JFrame();
	private Container mainContainer = mainFrame.getContentPane();

	/* the construct function */
	public SpringInvokerClient() {
		mainFrame.setSize(400, 300);
		mainFrame.setTitle("Spring Invoker Client");
		mainFrame.setLocationRelativeTo(null);
		mainContainer.setLayout(new GridLayout(5, 1, 5, 5));// the Container
															// uses the
															// GridLayout for 3
															// JPanels

		JPanel userNamePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));// each
																				// JPanel
																				// uses
																				// the
																				// FlowLayout
		JPanel userPasswordPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JPanel actionPanel = new JPanel(new FlowLayout());

		JLabel userNameLabel = new JLabel("用户名：");
		final JTextField userNameField = new JTextField(10);
		JLabel userPasswordLabel = new JLabel("密   码：");
		final JPasswordField userPasswordField = new JPasswordField(10);
		userPasswordField.setEchoChar((char) 0);// set the display words as
												// visible.
		final JButton sumbitButton = new JButton("提交");
		final JButton resetButton = new JButton("重置");

		userNamePanel.add(userNameLabel);
		userNamePanel.add(userNameField);
		userPasswordPanel.add(userPasswordLabel);
		userPasswordPanel.add(userPasswordField);
		actionPanel.add(sumbitButton);
		actionPanel.add(resetButton);

		mainContainer.add(userNamePanel);
		mainContainer.add(userPasswordPanel);
		mainContainer.add(actionPanel);

		sumbitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String userName = userNameField.getText();
				char[] userPassword = userPasswordField.getPassword();
				System.out.println(userName);
				System.out.println(userPassword);

				AbstractClient client = new AbstractClient();
				User result = client.login(userName, String.valueOf(userPassword));

				if (result != null) {
					String message=result.getName() + "登录成功 ！ age:" + result.getAge()+" clientId:"+result.getSessionId();
					JOptionPane.showMessageDialog(mainFrame, message,
							"Message", JOptionPane.INFORMATION_MESSAGE);
					// System.exit(0);//正常退出（等同于点击关闭窗口时执行frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
					// );）
				} else {
					JOptionPane.showMessageDialog(mainFrame,
							"You had input a wrong userID !!", "Warning", JOptionPane.WARNING_MESSAGE);
				}
				Arrays.fill(userPassword, '0'); // Zero out the possible
												// password, for security.
			}
		});

		resetButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {// set all the fields
														// empty.
				userNameField.setText("");
				userPasswordField.setText("");
			}
		});
		// mainFrame.pack();//automatically resize all the components to their
		// preferred sizes.
		mainFrame.setVisible(true);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new SpringInvokerClient();
	}

}
